﻿using BlogDemo.Core.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Text;

namespace BlogDemo.Infrastructure.Configuration
{
    public class PostConfiguration : IEntityTypeConfiguration<Post>
    {
        public void Configure(EntityTypeBuilder<Post> builder)
        {
            builder.HasKey(x => x.Id);
            builder.Property(x => x.Id).IsRequired();
            builder.Property(x => x.Title).IsRequired().HasColumnType("nvarchar(100)");
            builder.Property(x => x.Body).IsRequired().HasColumnType("nvarchar(max)");
            builder.Property(x => x.Author).HasColumnType("nvarchar(256)");
            builder.Property(x => x.Author).HasMaxLength(200);

            //Blog -> Post （一对多）
            builder.HasOne<Blog>()
                .WithMany()
                .HasForeignKey(p => p.BlogId)
                .OnDelete(DeleteBehavior.Cascade);

            //builder.HasMany(p => p.Tags)
            //    .WithMany(p => p.Posts)
            //    .UsingEntity(j => j.ToTable("PostTags"));

             //builder.UsingEntity<Dictionary<string, object>>("PostTag",
            //    j => j
            //.HasOne<Tag>()
            //.WithMany()
            //.HasForeignKey("TagId")
            //.HasConstraintName("FK_PostTag_Tags_TagId"),
            //j => j
            //.HasOne<Post>()
            //.WithMany()
            //.HasForeignKey("PostId")
            //.HasConstraintName("FK_PostTag_Posts_PostId")
         //);

        }
    }
}
